package com.cppsu.cgx.libraryseatselection.controller;

import com.cppsu.cgx.libraryseatselection.common.api.CommonResult;
import com.cppsu.cgx.libraryseatselection.dto.AvailableSeatsParam;
import com.cppsu.cgx.libraryseatselection.dto.SeatParam;
import com.cppsu.cgx.libraryseatselection.entity.Seat;
import com.cppsu.cgx.libraryseatselection.services.SeatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 17:47 2021/3/10
 * @Modified By:
 */
@Controller
@Api(tags = "座位管理")
@RequestMapping("/seat")
@CrossOrigin(origins = "*",maxAge = 3600)
public class SeatController {
    @Autowired
    SeatService seatService;

    @ApiOperation("批量新增")
    @PostMapping(value = "/addBatch")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    CommonResult<Integer> addBatch(Long floorId){
        return CommonResult.success(seatService.addBatch(floorId));
    }

    @ApiOperation("新增")
    @PostMapping(value = "/add")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    CommonResult<Integer> addBatch(@Valid @RequestBody SeatParam seatParam){
        return CommonResult.success(seatService.add(seatParam));
    }

    @ApiOperation("删除")
    @PostMapping(value = "/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    CommonResult<Integer> delete(List<Long> ids){
        return CommonResult.success(seatService.delete(ids));
    }

    @ApiOperation("更新")
    @PostMapping(value = "/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    CommonResult<Integer> update(SeatParam seatParam){
        return CommonResult.success(seatService.update(seatParam));
    }

    @ApiOperation("根据条件查询")
    @GetMapping(value = "/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin','user')")
    CommonResult<List<Seat>> list(SeatParam seatParam){
        return CommonResult.success(seatService.list(seatParam));
    }

    @ApiOperation("查询可用座位")
    @PostMapping(value = "/listAvailableSeats")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('admin','user')")
    CommonResult<List<Seat>> listAvailableSeats(@RequestBody @Valid AvailableSeatsParam availableSeatsParam){
        return CommonResult.success(seatService.listAvailableSeats(availableSeatsParam));
    }
}
